﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;

namespace Prism.Core.WPF
{
    public static class Extenstion
    {
        public static BitmapImage CreateBitmapImage(this Stream source)
        {
            if (source == null)
                return null;
            try
            {
                source.Position = 0;
                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.StreamSource = source;
                bitmapImage.EndInit();
                return bitmapImage;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
                return null;
            }
        }

        public static BitmapImage CreateBitmapImage(this Uri source)
        {
            var fs = new FileStream(source.LocalPath, FileMode.Open);
            var r = fs.CreateBitmapImage();
            fs.Close();
            return r;
        }

        public static BitmapImage CreateBitmapImage(this byte[] source)
        {
            if (source == null)
                return null;
            var ms = new MemoryStream(source);
            var r = CreateBitmapImage(ms);
            ms.Close();
            return r;
        }

        public static byte[] GetImageArray(this BitmapSource source)
        {
            if (source == null)
                return null;
            var ms = new MemoryStream();
            var encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(source));
            encoder.Save(ms);
            byte[] r = ms.ToArray();
            ms.Close();
            return r;
        }

        public static byte[] GetImageArray(this System.Windows.FrameworkElement element)
        {
            var target = element.CreateBitmapSource();
            return target.GetImageArray();
        }

        public static BitmapSource CreateBitmapSource(this System.Windows.FrameworkElement element)
        {
            var width = (int)element.ActualWidth;
            var height = (int)element.ActualHeight;

            var dpiX = 96d;
            var dpiY = 96d;
            var transform = element.LayoutTransform as ScaleTransform;
            if (transform != null)
            {
                dpiX /= transform.ScaleX;
                dpiY /= transform.ScaleY;
            }

            // only use default...
            var pixelFormat = PixelFormats.Default;
            //var stride = width * pixelFormat.BitsPerPixel * 8;
            //var buffer = new byte[stride * height];

            var target = new RenderTargetBitmap(width, height, dpiX, dpiY, pixelFormat);
            target.Render(element);
            return target;
        }
    }
}
